package logic

import (
	"context"
	"github.com/pkg/errors"
	"mall/common/xerr"

	"mall/app/product/cmd/rpc/internal/svc"
	"mall/app/product/cmd/rpc/pb"

	"github.com/zeromicro/go-zero/core/logx"
)

type GetAttrListByCategoryIdLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewGetAttrListByCategoryIdLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetAttrListByCategoryIdLogic {
	return &GetAttrListByCategoryIdLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// GetAttrListByCategoryId 获取某一个分类的所有参数
func (l *GetAttrListByCategoryIdLogic) GetAttrListByCategoryId(in *pb.GetAttrListByCategoryIdReq) (*pb.GetAttrListByCategoryIdResp, error) {
	list, err := l.svcCtx.AttrModel.FindByPageAndKey(l.ctx, in.Key, in.CategoryId, in.Limit, in.Page)
	if err != nil {
		return nil, errors.Wrapf(xerr.NewErrorWithCode(xerr.DbError), "err: %v", err)
	}
	ansList := make([]*pb.AttrItem, len(list))
	for i, v := range list {
		ansList[i] = &pb.AttrItem{
			AttrId:      v.AttrId,
			AttrName:    v.AttrName,
			SearchType:  v.SearchType,
			Icon:        v.Icon,
			ValueSelect: v.ValueSelect,
			AttrType:    v.AttrType,
			Enable:      v.Enable,
			ShowDesc:    v.ShowDesc,
			GroupName:   v.AttrName,
			CategoryId:  v.CatelogId,
		}
	}
	return &pb.GetAttrListByCategoryIdResp{
		List: ansList,
	}, nil
}
